package cn.edu.gsau.info.controller;

import cn.edu.gsau.info.models.Code;
import cn.edu.gsau.info.models.ResponseResult;
import cn.edu.gsau.info.models.Rongyuchenghao;
import cn.edu.gsau.info.models.User;
import cn.edu.gsau.info.service.IRongyuchenghaoService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jinjiang
 * @since 2022-11-17
 */
@RestController
@RequestMapping("/rongyuchenghao")
public class RongyuchenghaoController {

    @Autowired
    IRongyuchenghaoService rongyuchenghaoService;

    @Value("${gsau.upload.path}")
    private String uploadfile;

    @PostMapping("/save")
    public ResponseResult save(@RequestBody Rongyuchenghao rongyuchenghao){
        System.out.println(rongyuchenghao.getFileList());
        rongyuchenghaoService.add(rongyuchenghao);

        return new ResponseResult(Code.SAVE_OK,"保存成功",null);
    }
    @GetMapping("/{id}")
    public String getById(@PathVariable Integer id){
        System.out.println("id==>" + id);
        return "success";
    }
    @PostMapping("/upload")
    public ResponseResult upload(MultipartFile file, HttpServletRequest request){
        User user = new User();
        user.setId(6L);
        user.setUserName("ylz");
        File path = new File("D:/upload/" + user.getUserName());
        if(path.exists()==false){
            path.mkdirs();
        }
        HashMap<String,String> map  = new HashMap<>();

        String originalFilename = file.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileName = UUID.randomUUID().toString();
        System.out.println(fileName + suffix);
        try {
            file.transferTo(new File(path, fileName + suffix));
            String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/" + "files" + "/" + user.getUserName() + "/" + fileName + suffix;
            map.put("url",url);
            System.out.println(url);
        } catch (IOException e) {
            e.printStackTrace();
        }
        map.put("oldfilename",originalFilename);
        map.put("newfilename",fileName+suffix);


        return new ResponseResult(Code.SAVE_OK,"上传成功",map);
    }

    @DeleteMapping("/delete/{id}")
    public ResponseResult deleteById(@PathVariable Integer id){
        boolean delete = rongyuchenghaoService.removeById(id);
        return new ResponseResult(delete==true? Code.DELETE_OK:Code.DELETE_ERR,delete==true? "删除成功":"删除失败",null);
    }
    @PutMapping("/update")
    public ResponseResult update(@RequestBody Rongyuchenghao rongyuchenghao){
        boolean update =rongyuchenghaoService.updateById(rongyuchenghao);
        return new ResponseResult(update==true? Code.UPDATE_OK:Code.UPDATE_ERR,update==true? "更新成功":"更新失败",null);
    }
    @GetMapping("/findlike/{string}/{status}")
    public ResponseResult findLike(@PathVariable String string,@PathVariable String status){
        String str = "%"+string+"%";
        List<Rongyuchenghao> findLike = rongyuchenghaoService.selectLike(str,status);
        return new ResponseResult(Code.GET_OK,"查询成功",findLike);
    }
    @GetMapping("/findbystatus/{status}")
    public ResponseResult findByStatus(@PathVariable String status){
        List<Rongyuchenghao> findByStatus = rongyuchenghaoService.selectByStatus(status);
        return new ResponseResult(Code.GET_OK,"查询成功",findByStatus);
    }
    @GetMapping("/findbystr/{string}")
    public ResponseResult fingByStr(@PathVariable String string){
        String str="%"+string+"%";
        List<Rongyuchenghao> findByStr = rongyuchenghaoService.selectByStr(str);
        return new ResponseResult(Code.GET_OK,"查询成功",findByStr);
    }
    @GetMapping("/find/{id}")
    public ResponseResult findById(@PathVariable int id){
        Rongyuchenghao findById = rongyuchenghaoService.selectByRongyuchenghaoId(id);
        return new ResponseResult(Code.GET_OK,"查询成功",findById);
    }
    @GetMapping("/findall")
    public ResponseResult findAll(){
        User user = new User();
        user.setId(6L);
        user.setUserName("ylz");
        IPage rongyuchenghaos =rongyuchenghaoService.selectById(user.getId());
        return new ResponseResult(Code.GET_OK,"查询成功",rongyuchenghaos);
    }
    @GetMapping("/check/{id}")
    public ResponseResult selectByRongyuchenghaoId(@PathVariable int id){
        Rongyuchenghao checkById = rongyuchenghaoService.selectByRongyuchenghaoId(id);
        return new ResponseResult(Code.GET_OK,"查询成功",null);
    }
}

